package test.jsontojavabean;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonToJavaBean {

	public static void main(String[] args) {

		String str="{\"student\":[{\"name\":\"leilei\",\"age\":21},{\"name\":\"leilei02\",\"age\":23}]}";
	
		List<Student> list = null;

		try {

			ObjectMapper objectMapper=new ObjectMapper();
			StudentList studentList=objectMapper.readValue(str, StudentList.class);
			list= studentList.getStudent();

		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} 

		for(Student s:list){
			
			//System.out.println(s.getName());

			System.out.println(s.getName()+"   "+s.getAge());

		}
		
		test();

	}
	
	public static void test() {
        ArrayList<Student> list=new ArrayList<Student>();
        Student s1=new Student();
        s1.setName("leilei");
        s1.setAge("23");
        Student s2=new Student();
        s2.setName("leilei02");
        s2.setAge("31");
        list.add(s1);
        list.add(s2);
         
        StringWriter str=new StringWriter();
         
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            objectMapper.writeValue(str, list);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        System.out.println(str);
    }

}
